import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude, Expose, Transform } from 'class-transformer';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { Role } from './role.entity';

// 实体是数据库表
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  @ApiProperty({ description: '用户Id', example: '1' })
  id: number;
  @Column({ length: 50, unique: true })
  @ApiProperty({ description: '用户名', example: 'nick' })
  username: string;
  @Column()
  // @ApiProperty({description:'密码',  example:'123123'})
  @ApiHideProperty() // 隐藏字段不返回swagger中
  @Exclude()
  password: string;
  @Column({ length: 15, nullable: true })
  @Transform(({ value }) =>
    value ? value.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3') : value,
  )
  @ApiProperty({ description: '手机号', example: '13122222222' })
  mobile: string;

  @ManyToMany(() => Role)
  @JoinTable()
  roles: Role[];

  @Expose()
  @ApiProperty({ description: '联系方式', example: '邮箱:312312@qq.com' })
  get contact() {
    // 虚拟属性
    return `邮箱：${this.email}`;
  }
  @Column({ length: 100, unique: true })
  @ApiProperty({ description: '邮箱', example: '21321@qq.com' })
  email: string;
  @Column({ default: 1 }) // 是否生效0无效1生效
  @ApiProperty({ description: '生效状态', example: 1 })
  status: number;
  @Column({ default: false })
  @ApiProperty({ description: '是否超级管理员', example: true })
  is_super: boolean;
  @Column({ default: 100 })
  @ApiProperty({ description: '排序号', example: 100 })
  sort: number;

  @CreateDateColumn()
  @ApiProperty({ description: '创建时间', example: '时间' })
  createdAt: Date;

  @UpdateDateColumn()
  @ApiProperty({ description: '更新时间', example: '时间' })
  updatedAt: Date;
}
